package com.example.hystrix.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@DefaultProperties(defaultFallback = "fallback")
public class HystrixController {


    //降级
//    @HystrixCommand(fallbackMethod = "fallback")
    @RequestMapping("/age")
    //降级 配置文件方式
    @HystrixCommand
    public String findAge(){
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.getForObject("http://127.0.0.1:8082/girl/age",String.class);
        return response;
    }

    //熔断
    @HystrixCommand(commandProperties = {
        @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),
        @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),
        @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),
        @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60")
    })
    @RequestMapping("/ageTwo")
    public String findAgeTwo(Integer num){
        if(num%2==0){
            return "success";
        }
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.getForObject("http://127.0.0.1:8082/girl/age",String.class);
        return response;
    }

    private String fallback() {
        return "fallback enable, please wait!";
    }
}
